/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
$(function () {
    //修改数量
    $(".decrement").click(function () {
//        alert(1);
        var input = $(this).siblings(".goodsCount");
        var sum = parseInt(input.val());
        if (sum > 1) {
            sum -= 1;
        } else {
            sum =1;
            return false;
        }
        input.val(sum);
        //调用修改小计的方法
        priceCount();
        //调用ajax修改商品数量的方法
        updateCar($(this), "dec", "/shopcar/update");
        return false;    
    });
    $(".increment").click(function () {
//        alert(1);
       var input = $(this).siblings(".goodsCount");
       var sum = parseInt(input.val());
       sum += 1;
       input.val(sum);
       //调用修改小计的方法
       priceCount();
       //调用ajax修改商品数量的方法
       updateCar($(this), "inc", "/shopcar/update");
       return false;
    });
    
    //修改小计
    function priceCount() {
        var total = 0;
        $(".goodsCount").each(function (i) {
            var num = parseInt($(this).val());
//            alert(num);
            var price = parseFloat($(".goodsPrice").eq(i).text());
//            alert($("goodsPrice"));
            var sum = num*price;
            $(".pc").eq(i).text(sum);
            total += sum;
            
        });
        //修改总计
        $(".total").text(total);
    }
    
    //发送ajax修改数据库中的商品数量
    function updateCar(obj, act, url) {
        var id = obj.attr("data-id");
        $.ajax({
            type : "get",
            url : url,
            data : {id:id, act:act},
            success : function (msg) {
                if (msg.status !== 1) {
                    alert(msg.info);
                    location.reload();
                }
            } 
        });
    }
});
